<template>
  <div>
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/statistics' }"
        >账单统计</el-breadcrumb-item
      >
      <el-breadcrumb-item>所有金额</el-breadcrumb-item>
    </el-breadcrumb>

    <el-card>
      <!-- 搜索与添加区域 -->
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input
            placeholder="请输入搜索的用户"
            v-model="queryInfo.query"
            clearable
            @clear="getAmounts"
          >
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="getAmounts"
            ></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button
            type="primary"
            @click="addDialogVisible = true"
            v-show="addButton"
            >添加存款</el-button
          >
        </el-col>
      </el-row>

      <el-row :gutter="20" class="secondElRow">
        <el-col :span="12">
          <span>本月存款：{{ this.totalMoney }}元</span>
        </el-col>
      </el-row>

      <el-divider></el-divider>

      <!-- 用户列表区域 -->
      <el-table
        :data="amountList"
        border
        stripe
        :header-cell-style="{ 'text-align': 'center' }"
        :cell-style="{ 'text-align': 'center' }"
      >
        <el-table-column type="index"></el-table-column>
        <el-table-column label="姓名" prop="username"></el-table-column>
        <el-table-column label="存款" prop="totalMoney"></el-table-column>
        <el-table-column label="微信" prop="wx"></el-table-column>
        <el-table-column label="支付宝" prop="zfb"></el-table-column>
        <el-table-column label="基金-微信" prop="wxFund">
          <template slot-scope="scope">
            <el-popover
              placement="top-start"
              title="基金-微信详情"
              width="200"
              trigger="hover"
            >
              <p
                v-bind="wxFundContent"
                v-for="(item, index) in wxFundContent"
                :key="index"
              >
                {{ item.fundName }} : {{ item.currentMoney }} 元
              </p>

              <div
                slot="reference"
                @mouseenter="enterWxFund(scope.row)"
                @mouseleave="leave(scope.row)"
              >
                {{ scope.row.bank }}
              </div>
            </el-popover>
          </template>
        </el-table-column>
        <el-table-column label="基金-支付宝" prop="zfbFund">
          <template slot-scope="scope">
            <el-popover
              placement="top-start"
              title="基金-支付宝详情"
              width="200"
              trigger="hover"
            >
              <p
                v-bind="zfbFundContent"
                v-for="(item, index) in zfbFundContent"
                :key="index"
              >
                {{ item.fundName }} : {{ item.currentMoney }} 元
              </p>

              <div
                slot="reference"
                @mouseenter="enterZfbFund(scope.row)"
                @mouseleave="leave(scope.row)"
              >
                {{ scope.row.bank }}
              </div>
            </el-popover>
          </template>
        </el-table-column>
        <el-table-column label="银行卡" prop="bank">
          <template slot-scope="scope">
            <el-popover
              placement="top-start"
              title="银行卡详情"
              width="200"
              trigger="hover"
            >
              <p
                v-bind="bankContent"
                v-for="(item, index) in bankContent"
                :key="index"
              >
                {{ item.bankTypeName }} : {{ item.money }} 元
              </p>

              <div
                slot="reference"
                @mouseenter="enterBankType(scope.row)"
                @mouseleave="leave(scope.row)"
              >
                {{ scope.row.bank }}
              </div>
            </el-popover>
          </template>
        </el-table-column>
        <el-table-column label="创建时间" prop="createTime"></el-table-column>
        <el-table-column label="操作" width="180px">
          <template slot-scope="scope">
            <!-- 修改按钮 -->
            <el-button
              type="primary"
              icon="el-icon-edit"
              size="mini"
              @click="showEditDialog(scope.row.id)"
            ></el-button>
            <!-- 删除按钮 -->
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              @click="removeAmountById(scope.row.id)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pageNum"
        :page-sizes="[1, 2, 5, 10]"
        :page-size="queryInfo.pageSize"
        layout="->,total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>

    <!-- 添加存款的对话框 -->
    <el-dialog
      title="添加存款"
      :visible.sync="addDialogVisible"
      width="50%"
      @close="addDialogClosed"
    >
      <el-divider></el-divider>
      <!-- 内容主体区域 -->
      <el-form
        :model="addForm"
        :rules="formRules"
        ref="addFormRef"
        label-width="100px"
        size="mini"
      >
        <el-form-item label="微信余额" prop="wx">
          <el-input
            v-model="addForm.wx"
            placeholder="请输入微信余额"
          ></el-input>
        </el-form-item>
        <el-form-item label="支付宝余额" prop="zfb">
          <el-input
            v-model="addForm.zfb"
            placeholder="请输入支付宝余额"
          ></el-input>
        </el-form-item>
        <el-form-item label="贷款余额" prop="loan">
          <el-input
            v-model="addForm.loan"
            placeholder="请输入贷款余额"
          ></el-input>
        </el-form-item>
      </el-form>
      <!-- 底部区域 -->
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="addAmount">确 定</el-button>
        <el-button @click="addDialogVisible = false">取 消</el-button>
      </span>
    </el-dialog>

    <!-- 编辑存款 -->
    <el-dialog
      title="更新存款"
      :visible.sync="editDialogVisible"
      width="50%"
      @close="editDialogClosed"
    >
      <el-divider></el-divider>
      <!-- 内容主体区域 -->
      <el-form
        :model="editForm"
        :rules="formRules"
        ref="editFormRef"
        label-width="100px"
        size="mini"
      >
        <el-form-item label="微信余额" prop="wx">
          <el-input
            v-model="editForm.wx"
            placeholder="请输入微信余额"
          ></el-input>
        </el-form-item>
        <el-form-item label="支付宝余额" prop="zfb">
          <el-input
            v-model="editForm.zfb"
            placeholder="请输入支付宝余额"
          ></el-input>
        </el-form-item>
        <el-form-item label="贷款余额" prop="zfbFund">
          <el-input
            v-model="editForm.loan"
            placeholder="请输入贷款余额"
          ></el-input>
        </el-form-item>
      </el-form>
      <!-- 底部区域 -->
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="editAmount">确 定</el-button>
        <el-button @click="editDialogVisible = false">取 消</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      total: 0,
      queryInfo: {
        query: '',
        userId: window.sessionStorage.getItem('token'),
        // 当前的页数
        pageNum: 1,
        // 当前每页显示多少条数据
        pageSize: 10,
        startTime: '',
        endTime: ''
      },
      amountList: [],
      totalMoney: 0,
      // 控制添加用户对话框的显示与隐藏
      addDialogVisible: false,
      editDialogVisible: false,
      // 添加基金的表单数据
      addForm: {
        userId: window.sessionStorage.getItem('token'),
        wx: '',
        zfb: '',
        loan: ''
      },
      // 添加表单的验证规则对象
      formRules: {
        wx: [{ required: true, message: '请输入购买金额', trigger: 'blur' }],
        zfb: [{ required: true, message: '请输入当前金额', trigger: 'blur' }]
      },
      editForm: {
        id: '',
        userId: '',
        wx: '',
        zfb: '',
        loan: ''
      },
      bankContent: [],
      wxFundContent: [],
      zfbFundContent: []
    }
  },
  created() {
    this.getAmounts()
    this.getTotalMoney()
  },
  computed: {
    addButton() {
      return window.sessionStorage.getItem('token') !== '-1'
    }
  },
  methods: {
    // 获取所有的金额
    async getAmounts() {
      const { data: res } = await this.$http.get('amount', {
        params: this.queryInfo
      })
      if (res.code !== 1) {
        return this.$message.error('获取所有存款失败！')
      }
      this.amountList = res.data.pageList
      this.total = res.data.total
    },

    async getTotalMoney() {
      const userId = window.sessionStorage.getItem('token')
      const { data: res } = await this.$http.get(
        '/home/getTotalMoney?userId=' + userId
      )
      if (res.code !== 1) {
        return this.$message.error('获取存款失败！')
      }
      console.log(res)
      this.totalMoney = res.data
    },
    async showEditDialog(id) {
      const { data: res } = await this.$http.get('/amount/getAmountById/' + id)

      if (res.code !== 1) {
        return this.$message.error('查询存款信息失败！')
      }

      this.editForm = res.data
      this.editDialogVisible = true
    },

    async removeAmountById(id) {
      const { data: res } = await this.$http.delete('/amount', {
        params: { ids: id }
      })
      if (res.code !== 1) {
        return this.$message.error('删除存款失败！')
      }
      this.$message.success('删除存款成功！')
      this.getAmounts()
    },

    addAmount() {
      this.$refs.addFormRef.validate(async (valid) => {
        if (!valid) {
          this.$message.warning('必填项不能为空')
          return
        }

        const { data: res } = await this.$http.post('/amount', this.addForm)
        if (res.code !== 1) {
          return this.$message.error('获取存款失败！')
        }
        console.log(res)
        this.addDialogVisible = false
        this.$refs.addFormRef.resetFields()
        this.getAmounts()
      })
    },

    editAmount() {
      this.$refs.editFormRef.validate(async (valid) => {
        if (!valid) {
          this.$message.warning('必填项不能为空')
          return
        }

        const { data: res } = await this.$http.post(
          '/amount/updateAmount',
          this.editForm
        )

        if (res.code !== 1) {
          this.$message.error('更新存款失败！')
        }

        this.$message.success('更新基金成功！')
        // 隐藏添加用户的对话框
        this.editDialogVisible = false
        this.$refs.editFormRef.resetFields()
        // 重新获取用户列表数据
        this.getAmounts()
      })
    },

    enterBankType(row) {
      const banks = row.banks
      var obj = {}

      banks.forEach((item) => {
        if (item.bankType === 1) {
          obj = {
            id: item.bankType,
            bankTypeName: '中国银行',
            money: item.money
          }
          this.bankContent.push(obj)
        } else if (item.bankType === 2) {
          obj = {
            id: item.bankType,
            bankTypeName: '中国农业银行',
            money: item.money
          }
          this.bankContent.push(obj)
        } else if (item.bankType === 3) {
          obj = {
            id: item.bankType,
            bankTypeName: '中国建设银行',
            money: item.money
          }
          this.bankContent.push(obj)
        } else if (item.bankType === 4) {
          obj = {
            id: item.bankType,
            bankTypeName: '中国工商银行',
            money: item.money
          }
          this.bankContent.push(obj)
        } else if (item.bankType === 5) {
          obj = {
            id: item.bankType,
            bankTypeName: '中国交通银行',
            money: item.money
          }
          this.bankContent.push(obj)
        } else if (item.bankType === 6) {
          obj = {
            id: item.bankType,
            bankTypeName: '中国邮政银行',
            money: item.money
          }
          this.bankContent.push(obj)
        } else if (item.bankType === 7) {
          obj = {
            id: item.bankType,
            bankTypeName: '中国招商银行',
            money: item.money
          }
          this.bankContent.push(obj)
        } else if (item.bankType === 8) {
          obj = {
            id: item.bankType,
            bankTypeName: '兴业银行',
            money: item.money
          }
          this.bankContent.push(obj)
        } else if (item.bankType === 9) {
          obj = {
            id: item.bankType,
            bankTypeName: '广州银行',
            money: item.money
          }
          this.bankContent.push(obj)
        } else {
          obj = {
            id: item.bankType,
            bankTypeName: '其他银行',
            money: item.money
          }
          this.bankContent.push(obj)
        }
      })
    },
    enterWxFund(row) {
      this.wxFundContent = row.wxFunds
    },
    enterZfbFund(row) {
      this.zfbFundContent = row.zfbFunds
    },
    leave(row) {
      this.bankContent = []
      this.wxFundContent = []
      this.zfbFundContent = []
    },
    // 监听添加用户对话框的关闭事件
    addDialogClosed() {
      this.$refs.addFormRef.resetFields()
    },
    // 监听修改用户对话框的关闭事件
    editDialogClosed() {
      this.$refs.editFormRef.resetFields()
    },

    // 监听 pagesize 改变的事件
    handleSizeChange(newSize) {
      console.log(newSize)
      this.queryInfo.pageSize = newSize
      this.getAmounts()
    },
    // 监听 页码值 改变的事件
    handleCurrentChange(newPage) {
      console.log(newPage)
      this.queryInfo.pageNum = newPage
      this.getAmounts()
    }
  }
}
</script>

<style lang="less" scoped>
.secondElRow {
  margin-top: 15px;
  font-size: 12px;
  font-weight: 700;
}
.el-divider {
  margin-top: 8px;
}
.el-form .el-form-item .el-input {
  width: 240px;
}

.el-form {
  margin-left: 100px;
}
/deep/.el-dialog .el-dialog__footer {
  text-align: center;
  margin-top: -25px;
}
</style>
